package com.qf.ayc.shopcart.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.ayc.orders.entity.Orders;
import com.qf.ayc.shopcart.entity.Shopcart;
import com.qf.ayc.shopcart.service.IShopcartService;
import com.qf.ayc.utils.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author BeautyFairy
 * @since 2024-02-21
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/shopcart")
public class ShopcartController {
    @Autowired
    private IShopcartService iShopcartService;

    //---------------------------------------------------------------------------------------------------
    //获取购物车页面数据
    //localhost:8083/shopcart/shoppage?pageNum=2&limit=2
    @GetMapping("/shoppage")
    public ResultVo getshoplist(Integer pageNum, Integer limit) {
        List<Shopcart> shoplist = iShopcartService.getshoplist(pageNum, limit);
        if (shoplist != null) {
            return ResultVo.ok("1", "购物车列表", shoplist);
        } else {
            return ResultVo.ok("-1", "购物车没有这么多数据，需要减少页数");
        }
    }

    //---------------------------------------------------------------------------------------------------
    //查询某条页面数据
    // {
    //            "id": 1,
    //            "username": "猫猫侠",
    //            "productname": "皇冠猫粮",
    //            "productcount": 2,
    //            "productsku": "2公斤装",
    //            "attr": "8888",
    //            "totalprice": 100,
    //            "price": 50
    //        }
    @PostMapping("/ashoplist")
    public ResultVo ashoplist(@RequestBody Shopcart shopcart) {
        QueryWrapper<Shopcart> wrapper = new QueryWrapper<>();
        wrapper.eq(shopcart.getPrice() != null, "price", shopcart.getPrice())
                .eq(shopcart.getTotalprice() != null, "totalprice", shopcart.getTotalprice())
                .eq(shopcart.getProductname() != null && shopcart.getProductname() != "", "productname", shopcart.getProductname())
                .eq(shopcart.getProductcount() != null, "productcount", shopcart.getProductcount())
                .eq(shopcart.getProductsku() != null && shopcart.getProductsku() != "", "productsku", shopcart.getProductsku())
                .eq(shopcart.getUsername() != null && shopcart.getUsername() != "", "username", shopcart.getUsername());
        List<Shopcart> list = iShopcartService.list(wrapper);
        if (list != null) {
            return ResultVo.ok("1", "查询到购物车数据", list);
        } else {
            return ResultVo.ok("-1", "没查到对应的购物车数据");
        }

    }
    //---------------------------------------------------------------------------------------------------
    //添加一条购物车数据
    //localhost:8083/shopcart/add
    //{
    //
    //            "id": 1,
    //            "username": "旺旺大队开大会",
    //            "productname": "狗绳",
    //            "productcount": "2",
    //            "productsku": "抗咬",
    //            "attr": "1111",
    //            "totalprice": 110,
    //            "price":55
    //
    //}
    @PostMapping("/add")
    public ResultVo add(@RequestBody Shopcart shopcart){
        if(shopcart.getId()!=null) {
            shopcart.setId(null);
        }
        boolean save = iShopcartService.save(shopcart);
        if(save==true){
            return ResultVo.ok("1", "添加到购物车", shopcart);
        }else {
            return ResultVo.ok("-1", "没添加到购物车");
        }
    }
    //---------------------------------------------------------------------------------------------------
    //删除一条购物车数据
    //localhost:8083/shopcart/del?shopid=1
    @GetMapping("/del")
    public ResultVo del(String shopid){
        boolean b = iShopcartService.removeById(shopid);
        if(b){
            return ResultVo.ok("1", "删除此条购物车数据成功");
        }else{
            return ResultVo.ok("-1", "删除此条购物车数据失败");
        }
    }
    //---------------------------------------------------------------------------------------------------
    //更新一条购物车数据
    @PostMapping("/update")
    public ResultVo update(@RequestBody Shopcart shopcart){
        Integer id = shopcart.getId();
        log.info("id{}",id);
        boolean b = iShopcartService.updateById(shopcart);
        if(b){
            return ResultVo.ok("1", "更新此条购物车数据成功");
        }else{
            return ResultVo.ok("-1", "更新此条购物车数据失败");
        }
    }


    //获取购物车列表
    @GetMapping("/list")
    public ResultVo cartlist() {
        //1.打印日志
        log.info("订单");
        //2.进行必要的参数校验
        //3.通过service层的对象进行业务处理
        List<Shopcart> cartlist = iShopcartService.List();
        //4.返回json
        return ResultVo.ok("200", "所有订单信息", cartlist);
    }
}
